package Model.XML;

import java.util.*;

public class XMLWriter {
    
    private Stack stack = new Stack();
    private String indent = "   ";
    private StringBuilder xml = new StringBuilder();
    private String newLine = System.getProperty("line.separator");
    private char c = 34;
    
    public XMLWriter()  
    {
        xml.append("<?xml version=\"1.0\" ?>");
        xml.append(newLine);
    }
    
    public void Indent()
    {
        for(int i=0, j = stack.size(); i<j; i++)
            xml.append(indent);        
    }
    
    public void Push(String element)
    {
        this.Indent();
        xml.append("<" + element + ">");
        xml.append(newLine);
        stack.add(element);
    }
    
    public void Element(String element, String content)
    {
        this.Indent();
        xml.append("<" + element + ">");
        xml.append(content);
        xml.append("</" + element + ">");
        xml.append(newLine);
    }
    
    public void Element(String element, String content, String[] attributeName, String[] attribute)
    {
        this.Indent();
        xml.append("<" + element + " ");  
        for(int i=0; i<attributeName.length; i++)
            xml.append(attributeName[i] + " = "+ "\"" + attribute[i] + "\"" + " ");
        xml.append(">");
        xml.append(content);
        xml.append("</" + element + ">");
        xml.append(newLine);
    }
    
    public void Element(String element, String content, String attribute)
    {
        this.Indent();
        xml.append("<" + element + " id = " + c + attribute + c +">");
        xml.append(content);
        xml.append("</" + element + ">");
        xml.append(newLine);
    }
    
    public void Pop()
    {
        String element = (String)stack.pop();
        this.Indent();
        xml.append("</" + element + ">");
    }
    
    public String GetXML()
    {
        return xml.toString();
    }
}